package com.sixth.service.impl;

import com.sixth.bean.MarketUser;
import com.sixth.bean.MarketUserExample;
import com.sixth.bean.bo.WxAuthRegisterBo;
import com.sixth.bean.bo.WxAuthResetBo;
import com.sixth.constant.Constant;
import com.sixth.mapper.MarketUserMapper;
import com.sixth.service.WxAuthService;
import com.sixth.util.EncryptionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName WxAuthServiceImpl
 * @Description TODO
 * @Author XiaoWJ
 * @Date 2022/9/11 23:03
 **/
@Service
public class WxAuthServiceImpl implements WxAuthService {

    @Autowired
    MarketUserMapper marketUserMapper;


    @Override
    public int register(WxAuthRegisterBo wxAuthRegisterBo) {
        int affectRows = 0;

        String username = wxAuthRegisterBo.getUsername();

        // 判断手机号是否重复
        if (judgeIsExists(wxAuthRegisterBo.getMobile(),"mobile") > 0) {
            return Constant.REGISTER_ERROR_MOBILE_EXIST;
        }
        // 判断用户名是否重复
        if (judgeIsExists(username,"username") > 0) {
            return Constant.REGISTER_ERROR_USERNAME_EXIST;
        }

        MarketUser marketUser = new MarketUser();
        marketUser.setUsername(username);
        marketUser.setNickname(username);

        // TODO 暂时存储原密码

        marketUser.setPassword(wxAuthRegisterBo.getPassword());
        marketUser.setWeixinOpenid(wxAuthRegisterBo.getWxCode());
        marketUser.setMobile(wxAuthRegisterBo.getMobile());

        // TODO 最近一次登录时间和IP交由登录维护

        // 设置默认生日
        Date date = new Date();
        // 当前时间减去1000天
        date.setTime(date.getTime() - 1000 * 24 * 60 * 60 * 1000);
        marketUser.setBirthday(date);
        marketUser.setAddTime(new Date());
        marketUser.setUpdateTime(new Date());
        marketUser.setAvatar(Constant.DEFAULT_AVATARURL);

        affectRows = marketUserMapper.insertSelective(marketUser);

        return affectRows > 0 ? Constant.OK : Constant.NON;
    }

    @Override
    public void updateIPAndLoginTime(Integer id, String ip) {
        MarketUserExample example = new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();

        criteria.andIdEqualTo(id);

        MarketUser marketUser = new MarketUser();

        marketUser.setLastLoginTime(new Date());
        marketUser.setLastLoginIp(ip);

        marketUserMapper.updateByExampleSelective(marketUser, example);
    }

    @Override
    public int reset(WxAuthResetBo wxAuthResetBo) {
        // 先判断手机号是否被注册
        MarketUserExample example = new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();
        criteria.andMobileEqualTo(wxAuthResetBo.getMobile()).andDeletedEqualTo(false);

        List<MarketUser> marketUsers = marketUserMapper.selectByExample(example);
        if(marketUsers.size() == 0) {
            return Constant.NON;
        }
        // 手机号已注册，修改密码
        MarketUser marketUser = new MarketUser();

        // 用户名，用于密码加盐
        String username = marketUsers.get(0).getUsername();

        // 在此处对密码进行加密
        String password = EncryptionUtil.getMD5String(wxAuthResetBo.getPassword(),username);
        marketUser.setPassword(password);
        MarketUserExample example2 = new MarketUserExample();
        MarketUserExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andMobileEqualTo(wxAuthResetBo.getMobile()).andDeletedEqualTo(false);

        int affectRows = marketUserMapper.updateByExampleSelective(marketUser, example2);

        return affectRows > 0 ? Constant.OK : Constant.NON;
    }

    private int judgeIsExists(String value, String type) {
        MarketUserExample example = new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();

        if("username".equals(type)) {
            criteria.andUsernameEqualTo(value);
        }else if ("mobile".equals(type)){
            criteria.andMobileEqualTo(value);
        }
        criteria.andDeletedEqualTo(false);
        List<MarketUser> marketUsers2 = marketUserMapper.selectByExample(example);

        return marketUsers2.size();
    }
}
